package edu.cmu.cs.stage3.alice.core.behavior;

import edu.cmu.cs.stage3.alice.core.RenderTarget;
import edu.cmu.cs.stage3.alice.core.Variable;
import edu.cmu.cs.stage3.alice.core.property.ElementArrayProperty;
import edu.cmu.cs.stage3.alice.core.property.IntegerProperty;

public class KeyClickBehavior extends TriggerBehavior implements java.awt.event.KeyListener {

 private static Class[] s_supportedCoercionClasses = { KeyIsPressedBehavior.class };
 //Rem @Override
 public Class[] getSupportedCoercionClasses() {
  return s_supportedCoercionClasses;
 }

 public final IntegerProperty keyCode = new IntegerProperty( this, "keyCode", null );
 public final ElementArrayProperty renderTargets = new ElementArrayProperty( this, "renderTargets", null, edu.cmu.cs.stage3.alice.core.RenderTarget[].class );
 private edu.cmu.cs.stage3.alice.core.RenderTarget[] m_renderTargets = null;

 //Rem @Override
 public void manufactureAnyNecessaryDetails() {
  if( details.size()==1 ) {
   edu.cmu.cs.stage3.alice.core.Variable code = new edu.cmu.cs.stage3.alice.core.Variable();
   code.name.set( "code" );
   code.setParent( this );
   code.valueClass.set( Integer.class );
   details.add( code );
  }
 }

 //Rem @Override
 public void manufactureDetails() {
  super.manufactureDetails();
  edu.cmu.cs.stage3.alice.core.Variable keyChar = new edu.cmu.cs.stage3.alice.core.Variable();
  keyChar.name.set( "keyChar" );
  keyChar.setParent( this );
  keyChar.valueClass.set( Character.class );
  details.add( keyChar );

  edu.cmu.cs.stage3.alice.core.Variable code = new edu.cmu.cs.stage3.alice.core.Variable();
  code.name.set( "code" );
  code.setParent( this );
  code.valueClass.set( Integer.class );
  details.add( code );
 }

 private void updateDetails( java.awt.event.KeyEvent keyEvent ) {
  for( int i=0; i<details.size(); i++ ) {
   Variable detail = (Variable)details.get( i );
   if( detail.name.getStringValue().equals( "keyChar" ) ) {
    detail.value.set( new Character( keyEvent.getKeyChar() ) );
   } else if( detail.name.getStringValue().equals( "code" ) ) {
    detail.value.set( new Integer( keyEvent.getKeyCode() ) );
   }
  }
 }

 private boolean checkKeyCode( java.awt.event.KeyEvent keyEvent ) {
  int actualValue = keyEvent.getKeyCode();
  int requiredValue = keyCode.intValue( actualValue );
  return actualValue == requiredValue;
 }
 public void keyPressed( java.awt.event.KeyEvent keyEvent ) {
 }
 public void keyReleased( java.awt.event.KeyEvent keyEvent ) {
  updateDetails( keyEvent );
  if( checkKeyCode( keyEvent ) ) {
   trigger( keyEvent.getWhen()*0.001 );
  }
 }
 public void keyTyped( java.awt.event.KeyEvent keyEvent ) {
 }

 //Rem @Override
 protected void started( edu.cmu.cs.stage3.alice.core.World world, double time ) {
  super.started( world, time );
  m_renderTargets = (edu.cmu.cs.stage3.alice.core.RenderTarget[])renderTargets.get();
  if( m_renderTargets==null ) {
   m_renderTargets = (edu.cmu.cs.stage3.alice.core.RenderTarget[])world.getDescendants( edu.cmu.cs.stage3.alice.core.RenderTarget.class );
  }
  //LOB for (RenderTarget target : m_renderTargets)
  int t_tam = m_renderTargets!=null ? m_renderTargets.length : 0;
  for (int i_i=0; i_i<t_tam; i_i++) {
   m_renderTargets[i_i].addKeyListener( this ); //L target.addKeyListener( this );
  }
 }
 //Rem @Override
 protected void stopped( edu.cmu.cs.stage3.alice.core.World world, double time ) {
  super.stopped( world, time );
  //L for (RenderTarget target : m_renderTargets)
  int t_tam = m_renderTargets!=null ? m_renderTargets.length : 0;
  for (int i_i=0; i_i<t_tam; i_i++) {
   m_renderTargets[i_i].removeKeyListener( this ); //L target.removeKeyListener( this );
  }
  m_renderTargets = null;
 }

}
